package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.FileAdapter;
import flc.ast.databinding.ActivityPrivacyBinding;
import flc.ast.utils.Content;

public class PrivacyActivity extends BaseAc<ActivityPrivacyBinding> {

    public static String path;
    private String mCurrentRootPath;
    private FileAdapter fileAdapter;
    private List<String> mList = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_privacy;
    }

    @Override
    public void onBackPressed() {
        if (mCurrentRootPath.equals(PathUtils.getFilesPathExternalFirst() + path)) {
            super.onBackPressed();
        } else {
            mCurrentRootPath = FileUtils.getDirName(mCurrentRootPath);
            mCurrentRootPath = mCurrentRootPath.substring(0, mCurrentRootPath.length() - 1);
            getData(mCurrentRootPath);
        }
    }

    @Override
    protected void initView() {
        mDataBinding.ivPrivacyBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivAdd.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new LinearLayoutManager(mContext));
        fileAdapter = new FileAdapter();
        mDataBinding.rvFileList.setAdapter(fileAdapter);
        fileAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAdd://添加
                AlbumActivity.path = path;
                startActivity(AlbumActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        mCurrentRootPath = PathUtils.getFilesPathExternalFirst() + path;
        mDataBinding.tvPrivacyTitle.setText(FileUtils.getFileName(mCurrentRootPath));
        getData(mCurrentRootPath);
    }

    private void getData(String rootPath) {
        List<File> files = FileUtils.listFilesInDir(rootPath);
        if (files.size() == 0) {
            mDataBinding.tvFileNoData.setVisibility(View.VISIBLE);
            mDataBinding.rvFileList.setVisibility(View.GONE);
        } else {
            mDataBinding.tvFileNoData.setVisibility(View.GONE);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mList.clear();
            for (File file : files) {
                mList.add(file.getPath());
            }
            mDataBinding.rvFileList.scrollToPosition(0);
            fileAdapter.setList(mList);
        }
    }

}